﻿using System;
using System.Windows.Forms;

namespace ShareHelper
{
    /// <summary>
    /// 编辑框拖拽接收帮助类
    /// </summary>
    public static class DragHelper
    {
        /// <summary>
        /// 设置拖拽
        /// </summary>
        /// <param name="textBox"></param>
        public static void SetDrag(TextBox textBox)
        {
            textBox.AllowDrop = true;
            textBox.DragDrop += TextBox_Path_DragDrop;
            textBox.DragEnter += TextBox_Path_DragEnter;
        }

        private static void TextBox_Path_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private static void TextBox_Path_DragDrop(object sender, DragEventArgs e)
        {
            if (sender is TextBox textBox)
            {
                textBox.Text = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            }
        }
    }
}